Banker’s algorithm is named so because it is used in the banking system to check whether a loan can be sanctioned to a person or not. Suppose there are n number of account holders in a bank and the total sum of their money is S. If a person applies for a loan then the bank first subtracts the loan amount from the total money that the bank has and if the remaining amount is greater than S then only the loan is sanctioned. It is done because if all the account holders come to withdraw their money then the bank can easily do it. In other words, the bank would never allocate its money in such a way that it can no longer satisfy the needs of all its customers. The bank would try to be in a safe state always.
The algorithm for finding out whether or not a system is in a safe state can be described as follows: 1) Let Work and Finish be vectors of length ‘m’ and ‘n’ respectively. Initialize: Work = Available Finish[i] = false; for i=1, 2, 3, 4….n 2) Find an i such that both a) Finish[i] = false b) Needi <= Work if no such i exists goto step (4) 3) Work = Work + Allocation[i] Finish[i] = true goto step (2) 4) if Finish [i] = true for all i then the system is in a safe state
Let Requesti be the request array for process Pi. Requesti [j] = k means process Pi wants k instances of resource type Rj. When a request for resources is made by process Pi, the following actions are taken: 1) If Requesti <= Needi Goto step (2) ; otherwise, raise an error condition, since the process has exceeded its maximum claim. 2) If Requesti <= Available Goto step (3); otherwise, Pi must wait, since the resources are not available. 3) Have the system pretend to have allocated the requested resources to process Pi by modifying the state as follows: Available = Available – Requesti Allocationi = Allocationi + Requesti Needi = Needi– Requesti